package com.jy.horny.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.jy.horny.R

/**
 * 上传图片选择方式弹窗
 */
class UploadPhotoDialog private constructor(context: Context, theme: Int) : Dialog(context, theme) {

    override fun dismiss() {
        super.dismiss()
    }

    interface Callback {
        fun onBackClick(type: Int?)
    }

    private class Params {
        lateinit var context: Context
        var hasShadow = true
        var canCancel = true
        var callback: Callback? = null
    }

    class Builder(context: Context) {
        private val p = Params()

        init {
            p.context = context
        }

        fun setCallback(callback: Callback): Builder {
            p.callback = callback
            return this
        }

        fun create(): UploadPhotoDialog {
            val dialog = UploadPhotoDialog(
                p.context,
                if (p.hasShadow) com.lib.module_res.R.style.Theme_Light_NoTitle_Dialog else com.lib.module_res.R.style.Theme_Light_NoTitle_NoShadow_Dialog
            )

            val window = dialog.window
            if (window != null) {
                window.setWindowAnimations(com.lib.module_res.R.style.Animation_Bottom_Rising)
                window.decorView.setPadding(0, 0, 0, 0)
                val lp = window.attributes
                lp.width = WindowManager.LayoutParams.MATCH_PARENT
                lp.height = WindowManager.LayoutParams.MATCH_PARENT
                window.attributes = lp
                window.setGravity(Gravity.CENTER)
            }

            @SuppressLint("InflateParams") val view = LayoutInflater.from(p.context)
                .inflate(R.layout.dialog_upload_photo, null)

            initialViews(view, dialog)

            dialog.setContentView(view)
            dialog.setCanceledOnTouchOutside(p.canCancel)
            dialog.setCancelable(p.canCancel)
            return dialog
        }

        fun show() {
            create().show()
        }

        private fun initialViews(view: View, dialog: UploadPhotoDialog) {
            val dialogUploadAlbums = view.findViewById<TextView>(R.id.dialogUploadAlbums)
            val dialogUploadCamera = view.findViewById<TextView>(R.id.dialogUploadCamera)
            val dialogUploadCancel = view.findViewById<TextView>(R.id.dialogUploadCancel)

            dialogUploadAlbums.setOnClickListener {
                p.callback?.onBackClick(1)
                dialog.dismiss()
            }
            dialogUploadCamera.setOnClickListener {
                p.callback?.onBackClick(2)
                dialog.dismiss()
            }
            dialogUploadCancel.setOnClickListener {
                dialog.dismiss()
            }
        }
    }

}